const router = require('koa-router')()
const {
  list,
  registerUser,
  userLogin
} = require('../controller/user')
const {
  SuccessModel,
  ErrorModel
} = require('../model/ResModel')

router.prefix('/api/users')

//用户登录注册
router.post('/register', async (ctx, next) => {
  const {
    userName,
    gender,
    avatar,
    openId,
    sessionKey
  } = ctx.request.body
  console.log(JSON.stringify(ctx.request.body));
  ctx.body = await registerUser({
    userName,
    gender,
    avatar,
    openId,
    sessionKey
  })
})

//用户登录
router.get('/login',async (ctx,next) =>{
  const id = ctx.query.id
  ctx.body =await userLogin(id)
})

//用户列表
router.get('/list', async (ctx, next) => {
  const {
    province,
    city,
    types,
    pageIndex,
    pageSize
  } =  ctx.query //创建参数

  const listData = await list({
    province,
    city,
    types,
    pageIndex,
    pageSize
  })
  ctx.body = listData
})

module.exports = router
